package com.kiln.juxingqs.ui

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdthAdapter
import com.kiln.juxingqs.api.bean.WdthRet
import com.kiln.juxingqs.databinding.ActivityWdthBinding
import com.kiln.juxingqs.ui.vm.WdthViewModel
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.dialog.MsgTitleDialog

//我的th

class WdthActivity :
    BaseMiddleActivity<WdthViewModel, ActivityWdthBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_wdth
    }

    val adapter: WdthAdapter by lazy {
        WdthAdapter(this@WdthActivity).apply {
            shuohuo ={
                MsgTitleDialog(this@WdthActivity).apply {
                    title = "确认收货？"
                    okTxt = "确认"
                    cancleTxt = "关闭"
                    ok = {
                        viewModel.confirm_order(it)
                    }
                }.show()
            }
        }
    }

    override fun initViewModel(): WdthViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.recyclerView.adapter = adapter
        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.deliver_list as MutableList<WdthRet.Deliver?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.qrshCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("操作成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                }
            }
        })

    }

    fun searchClick() {
        if (viewModel.searchVal.value.isNullOrEmpty()) {
            toast("请输入关键词")
            return
        }
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }
}